package com.wise.gemmes.controller.equipment;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.web.system.pojo.base.TSDepart;
import org.jeecgframework.web.system.service.SystemService;
import org.jeecgframework.core.util.MyBeanUtils;

import com.wise.gemmes.entity.equipment.Gem_equip_typeEntity;
import com.wise.gemmes.service.equipment.Gem_equip_typeServiceI;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.jeecgframework.core.beanvalidator.BeanValidators;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;

import java.io.IOException;
import java.net.URI;
import org.springframework.http.MediaType;
import org.springframework.web.util.UriComponentsBuilder;

/**
 * @Title: Controller
 * @Description: 设备类型
 * @author zhangdaihao
 * @date 2018-03-14 09:11:58
 * @version V1.0
 *
 */
@Scope("prototype")
@Controller
@RequestMapping("/gem_equip_typeController")
public class Gem_equip_typeController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(Gem_equip_typeController.class);

	@Autowired
	private Gem_equip_typeServiceI gem_equip_typeService;
	@Autowired
	private SystemService systemService;
	@Autowired
	private Validator validator;

	private String message;

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	/**
	 * 设备类型列表 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "list")
	public ModelAndView list(HttpServletRequest request) {
		return new ModelAndView("com/wise/gemmes/equipment/gem_equip_typeList");
	}

	/**
	 * easyui AJAX请求数据
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param user
	 */

	@RequestMapping(params = "datagrid")
	public void datagrid(Gem_equip_typeEntity gem_equip_type, HttpServletRequest request, HttpServletResponse response,
			DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(Gem_equip_typeEntity.class, dataGrid);
		// 查询条件组装器
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, gem_equip_type,
				request.getParameterMap());
		this.gem_equip_typeService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}

	/**
	 * 删除设备类型
	 * 
	 * @return
	 */
	@RequestMapping(params = "del")
	@ResponseBody
	public AjaxJson del(Gem_equip_typeEntity gem_equip_type, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		gem_equip_type = systemService.getEntity(Gem_equip_typeEntity.class, gem_equip_type.getId());
		message = "设备类型删除成功";
		gem_equip_typeService.delete(gem_equip_type);
		systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);

		j.setMsg(message);
		return j;
	}

	/**
	 * 添加设备类型
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "save")
	@ResponseBody
	public AjaxJson save(Gem_equip_typeEntity gem_equip_type, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		if (StringUtil.isNotEmpty(gem_equip_type.getId())) {
			message = "设备类型更新成功";
			Gem_equip_typeEntity t = gem_equip_typeService.get(Gem_equip_typeEntity.class, gem_equip_type.getId());
			try {
				MyBeanUtils.copyBeanNotNull2Bean(gem_equip_type, t);
				gem_equip_typeService.saveOrUpdate(t);
				systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
			} catch (Exception e) {
				e.printStackTrace();
				message = "设备类型更新失败";
			}
		} else {
			message = "设备类型添加成功";
			gem_equip_typeService.save(gem_equip_type);
			systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
		}
		j.setMsg(message);
		return j;
	}

	/**
	 * 设备类型列表页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "addorupdate")
	public ModelAndView addorupdate(Gem_equip_typeEntity gem_equip_type, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(gem_equip_type.getId())) {
			gem_equip_type = gem_equip_typeService.getEntity(Gem_equip_typeEntity.class, gem_equip_type.getId());
			req.setAttribute("gem_equip_typePage", gem_equip_type);
		}
		return new ModelAndView("com/wise/gemmes/equipment/gem_equip_type");
	}

	@RequestMapping(method = RequestMethod.GET)
	@ResponseBody
	public List<Gem_equip_typeEntity> list() {
		List<Gem_equip_typeEntity> listGem_equip_types = gem_equip_typeService.getList(Gem_equip_typeEntity.class);
		return listGem_equip_types;
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	@ResponseBody
	public ResponseEntity<?> get(@PathVariable("id") String id) {
		Gem_equip_typeEntity task = gem_equip_typeService.get(Gem_equip_typeEntity.class, id);
		if (task == null) {
			return new ResponseEntity(HttpStatus.NOT_FOUND);
		}
		return new ResponseEntity(task, HttpStatus.OK);
	}

	@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public ResponseEntity<?> create(@RequestBody Gem_equip_typeEntity gem_equip_type, UriComponentsBuilder uriBuilder) {
		// 调用JSR303 Bean Validator进行校验，如果出错返回含400错误码及json格式的错误信息.
		Set<ConstraintViolation<Gem_equip_typeEntity>> failures = validator.validate(gem_equip_type);
		if (!failures.isEmpty()) {
			return new ResponseEntity(BeanValidators.extractPropertyAndMessage(failures), HttpStatus.BAD_REQUEST);
		}

		// 保存
		gem_equip_typeService.save(gem_equip_type);

		// 按照Restful风格约定，创建指向新任务的url, 也可以直接返回id或对象.
		String id = gem_equip_type.getId();
		URI uri = uriBuilder.path("/rest/gem_equip_typeController/" + id).build().toUri();
		HttpHeaders headers = new HttpHeaders();
		headers.setLocation(uri);

		return new ResponseEntity(headers, HttpStatus.CREATED);
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<?> update(@RequestBody Gem_equip_typeEntity gem_equip_type) {
		// 调用JSR303 Bean Validator进行校验，如果出错返回含400错误码及json格式的错误信息.
		Set<ConstraintViolation<Gem_equip_typeEntity>> failures = validator.validate(gem_equip_type);
		if (!failures.isEmpty()) {
			return new ResponseEntity(BeanValidators.extractPropertyAndMessage(failures), HttpStatus.BAD_REQUEST);
		}

		// 保存
		gem_equip_typeService.saveOrUpdate(gem_equip_type);

		// 按Restful约定，返回204状态码, 无内容. 也可以返回200状态码.
		return new ResponseEntity(HttpStatus.NO_CONTENT);
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	@ResponseStatus(HttpStatus.NO_CONTENT)
	public void delete(@PathVariable("id") String id) {
		gem_equip_typeService.deleteEntityById(Gem_equip_typeEntity.class, id);
	}
	//add
	//获取设备类型信息
	@ResponseBody
	@RequestMapping(params = "typelist")
	public void getTypeList(HttpServletRequest rq,HttpServletResponse rs) throws IOException{
		List<Gem_equip_typeEntity> list = null;
		String _typeid = rq.getParameter("_typeid");
		System.out.println(_typeid);
		if(_typeid != null && !_typeid.equals("")){
			list = systemService.findByProperty(Gem_equip_typeEntity.class,"id",_typeid);
		}else{
			list = systemService.getList(Gem_equip_typeEntity.class);
		}
		JSONArray jarray = new JSONArray();
		
		for(Gem_equip_typeEntity model : list){
			JSONObject jobject = new JSONObject();
			jobject.put("id", model.getId());
			jobject.put("text", model.getName());
			
			jarray.add(jobject);
		}
		System.out.println(jarray.toString());
		rs.getWriter().write(jarray.toString());
	}
	
	
}
